package com.ruoyi.bl.service.impl;

import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.bl.domain.BlOrderGoods;
import com.ruoyi.bl.mapper.BlOrderGoodsMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bl.mapper.BlOrderMapper;
import com.ruoyi.bl.domain.BlOrder;
import com.ruoyi.bl.service.IBlOrderService;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-11
 */
@Service
public class BlOrderServiceImpl implements IBlOrderService
{
    @Autowired
    private BlOrderMapper blOrderMapper;
    @Autowired
    private BlOrderGoodsMapper blOrderGoodsMapper;

    /**
     * 查询订单信息
     * 
     * @param orderId 订单信息主键
     * @return 订单信息
     */
    @Override
    public BlOrder selectBlOrderByOrderId(Long orderId)
    {
        return blOrderMapper.selectBlOrderByOrderId(orderId);
    }

    /**
     * 查询订单信息列表
     * 
     * @param blOrder 订单信息
     * @return 订单信息
     */
    @Override
    public List<BlOrder> selectBlOrderList(BlOrder blOrder)
    {
        List<BlOrder> blOrdersList = blOrderMapper.selectBlOrderList(blOrder);
        // 提取订单 ID 列表
        List<Long> blOrderIds = blOrdersList.stream()
                .map(BlOrder::getOrderId)
                .collect(Collectors.toList());

        // 批量查询商品
        List<BlOrderGoods> blOrderGoodsList = blOrderGoodsMapper.selectBlOrderGoodsByOrderIds(blOrderIds);

        // 将商品按订单 ID 分组
        Map<Long, List<BlOrderGoods>> blOrderGoodsMap = blOrderGoodsList.stream()
                .collect(Collectors.groupingBy(BlOrderGoods::getOrderId));

        // 将商品设置到订单中
        for (BlOrder blorder : blOrdersList) {
            blorder.setBlOrderGoodsList(blOrderGoodsMap.get(blorder.getOrderId()));
        }

        return blOrdersList;
//        return blOrderMapper.selectBlOrderList(blOrder);
    }

    /**
     * 新增订单信息
     * 
     * @param blOrder 订单信息
     * @return 结果
     */
    @Override
    public int insertBlOrder(BlOrder blOrder)
    {
        blOrder.setCreateTime(DateUtils.getNowDate());
        return blOrderMapper.insertBlOrder(blOrder);
    }

    /**
     * 修改订单信息
     * 
     * @param blOrder 订单信息
     * @return 结果
     */
    @Override
    public int updateBlOrder(BlOrder blOrder)
    {
        blOrder.setUpdateTime(DateUtils.getNowDate());
        return blOrderMapper.updateBlOrder(blOrder);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderIds 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteBlOrderByOrderIds(Long[] orderIds)
    {
        return blOrderMapper.deleteBlOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderId 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteBlOrderByOrderId(Long orderId)
    {
        return blOrderMapper.deleteBlOrderByOrderId(orderId);
    }

    @Override
    public List<BlOrder> selectBlOrderPsList(BlOrder blOrder) {

        List<BlOrder> blOrdersList = blOrderMapper.selectBlOrderPsList(blOrder);
        // 提取订单 ID 列表
        List<Long> blOrderIds = blOrdersList.stream()
                .map(BlOrder::getOrderId)
                .collect(Collectors.toList());

        // 批量查询商品
        List<BlOrderGoods> blOrderGoodsList = blOrderGoodsMapper.selectBlOrderGoodsByOrderIds(blOrderIds);

        // 将商品按订单 ID 分组
        Map<Long, List<BlOrderGoods>> blOrderGoodsMap = blOrderGoodsList.stream()
                .collect(Collectors.groupingBy(BlOrderGoods::getOrderId));

        // 将商品设置到订单中
        for (BlOrder blorder : blOrdersList) {
            blorder.setBlOrderGoodsList(blOrderGoodsMap.get(blorder.getOrderId()));
        }

        return blOrdersList;
        //return blOrderMapper.selectBlOrderPsList(blOrder);
    }
}
